// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { BuffManager } from "./battle/BuffManager";
import { AStar } from "./battle/findpath/AStar";
import { GridMap } from "./battle/findpath/Grid";
import GameWorld, { Group } from "./battle/GameWorld";
import MoveComponent from "./battle/MoveComponent";
import AimSkill from "./battle/skills/AimSkill";
import Unit from "./battle/Unit";
import UnitStateMachine, { UnitStateEnum } from "./battle/UnitStateMachine";
import ResManager from "./core/ResManager";

export const unitConfig = {
    1001: {
        atk: 20,
        maxhp: 500,
        moving_speed: 2,
        atk_speed: 0.5,
        atk_range: 6,
    },
    1002: {
        atk: 20,
        maxhp: 500,
        moving_speed: 2,
        atk_speed: 0.5,
        atk_range: 8,
    }
}
const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Node)
    map: cc.Node = null;

    units: Unit[] = [];
    isstart = false;
    world: GameWorld
    async start() {
        await ResManager.instance.loadDir('model');
        this.world = new GameWorld();
        this.world.mapNode = this.map;
        let group = new Group();
        group.groupId = 2;
        this.world.addGroup(group);
        let group1 = new Group();
        this.world.addGroup(group1);
        group1.groupId = 1;
        for (let index = 0; index < 100; index++) {

            let unit = Unit.creat(1002, 2, this.world);
            unit.pos = cc.v2(Math.floor(Math.random() * 20), Math.floor(Math.random() * 20));
            group.add(unit);
            this.units.push(unit);
        }
        for (let index = 0; index < 100; index++) {

            let unit1 = Unit.creat(1001, 100, this.world);
            unit1.pos = cc.v2(Math.floor(Math.random() * 20), Math.floor(Math.random() * 20));
            group1.add(unit1);
            this.units.push(unit1);
        }
        for (const iterator of this.units) {
            iterator.changeState(UnitStateEnum.FindAim);
        }
        this.isstart = true;

    }

    update(dt) {
        if (!this.isstart) {
            return;
        }
        this.world.update(dt);
    }
}
